#include "DL_CH.h"
#include "DL_Message_Handler.h"
#include "IO_LINK_DataBuffer.h"


CH_STATE_TYPE CH_STATE[PORT_NUM];

/*************输出EVent********************/

void Event_DL_Control_Ind_VALID(uint8_t Port){
	//通知上层输入有效
	//待更新
}

void  Event_DL_Control_Ind_INVALID(uint8_t Port){
	//通知上层输入无效
	//待更新
	
}

void DL_CH_Init(void){
    for(uint8_t i=0;i<PORT_NUM;i++){
        CH_STATE[i]=CH_Inactive_0;
    }
}

 

void DL_CH_T2(uint8_t Port){
    if(PDInStatus[Port]==0){
        Event_DL_Control_Ind_VALID(Port); //
    }else{
        Event_DL_Control_Ind_INVALID(Port);
    }
}


//在此函数中，为MC_Struct准备好数据，填充MC和OD
void DL_CH_T4(uint8_t Port){
    //准备OD.Req的工作在T5完成
	
}


void DL_CH_T5(uint8_t Port){
    //利用Command_BufferOut构造OD.Req
	  MC_Struct[Port].Dirction=Direction_Write;
    *MC_Struct[Port].MC=0x20;
    *MC_Struct[Port].OD=Command_BufferOut[Port];
	    
	  
    Event_OD(Port);
}

void Event_CH_Conf_ACTIVE(uint8_t Port){
    if (CH_STATE[Port]==CH_Inactive_0)
    {
        CH_STATE[Port]=CH_Idle_1;
       // DL_CH_T1(Port);
    }
    
}

void Event_CH_Conf_INACTIVE(uint8_t Port){
    
        CH_STATE[Port]=CH_Inactive_0;
        
    
}

void Event_PDInStatus(uint8_t Port){
    if(CH_STATE[Port]==CH_Idle_1){
        DL_CH_T2(Port);
    }
}

void Event_DL_Control_PDOUT(uint8_t Port){
    if(CH_STATE[Port]==CH_Idle_1){
        CH_STATE[Port]=CH_MasterCommand_2;
      //  DL_CH_T3(Port);
    }
}

void Event_CH_DL_Write_DEVICEMODE(uint8_t Port){
	      //通知CH做好准备
	  if(CH_STATE[Port]==CH_Idle_1){    
			   CH_STATE[Port]=CH_MasterCommand_2;
			   //DL_CH_T4(Port);
		 }
 
	
}

 void Event_CommandTrig(uint8_t Port){
    if(CH_STATE[Port]==CH_MasterCommand_2){
        CH_STATE[Port]=CH_Idle_1;
		  	//Event_Done_and_Not_EventActive(Port);// 通知OD状态机复位
			   Event_Done(Port);
        DL_CH_T5(Port);
    }
}


